<?php
class Recommendation_Popular extends Recommendation{
	
	public function __construct(){
		parent::__construct();
		$this->method_recommendation= RECOMMANDATION_POPULAIRE;
	}
	
	protected function makeQuery(){
		
		$id_user_clause= isset($_SESSION['infosAuth']) && (!empty($_SESSION['infosAuth'])) ? "LEFT JOIN Rating ON Title.id_title=Rating.id_title AND Rating.id_user='".$_SESSION['infosAuth']->getIdUser()."' INNER JOIN Popularity ON Title.id_title=Popularity.id_title WHERE Rating.id_title is NULL " : " INNER JOIN Popularity ON Title.id_title=Popularity.id_title ";
		
		/* Prérequete */
		$query = "SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM Title ".$id_user_clause;
		
		$db = Outils_Bd::getInstance()->getConnexion();
		$sth=$db->prepare($query);
		$sth->execute();
		$offset = $sth->fetch(PDO::FETCH_ASSOC);

		$requete = "SELECT Title.id_title, Title.name, Title.artist, Title.youtube_id FROM Title ".$id_user_clause." LIMIT ".$offset['offset'].", 1"; 
		
	
		
		return $requete;
	}
	
	public static function canBeInstanciate(){
		return true;	
	}
	
	public function getMethodRecommendation(){
		return $this->method_recommendation;
	}
	
}
?>